//
//  TagSelectVC.m
//  XV
//
//  Created by apple on 04/06/2019.
//  Copyright © 2019 XR. All rights reserved.
//

#import "TagSelectVC.h"
#import "TagSelectCollectionCell.h"


@interface TagSelectVC ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UILabel* titleLabel;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation TagSelectVC

static NSString *const TagSelectCollectionCellID = @"TagSelectCollectionCellID";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = TableViewBGColor;
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.collectionView];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(LL_StatusBarAndNavigationBarHeight+20);
        make.left.mas_equalTo(15);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(LL_StatusBarAndNavigationBarHeight+50);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.equalTo(self.view);
    }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(okButtonDidClick)];
    [self.navigationItem.rightBarButtonItem setTintColor:ColorHex(0x666666)];
    
    _titleLabel.text = [NSString stringWithFormat:@"选择你擅长话题(%ld/3)",self.selectTags.count];
}


-(void)okButtonDidClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(TagSelectVCDidSelect: vc:)])
    {
        [self.delegate TagSelectVCDidSelect:self.selectTags vc:self];
    }
}


#pragma mark - dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.tags.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TagSelectCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TagSelectCollectionCellID forIndexPath:indexPath];
    if (cell==nil)
    {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:TagSelectCollectionCellID forIndexPath:indexPath];
    }
    NSString* str = [self.tags objectAtIndex:indexPath.item];
    cell.name = str;
    cell.isSelected = [self isInSelecteTag:str];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* str = [self.tags objectAtIndex:indexPath.item];
    BOOL isSelected = [self isInSelecteTag:str];
    if (isSelected)
    {
        [self.selectTags removeObject:str];
    }
    else
    {
        if (self.selectTags.count>=3)
        {
            return;
        }
        
        [self.selectTags addObject:str];
    }
    
    _titleLabel.text = [NSString stringWithFormat:@"选择你擅长话题(%ld/3)",self.selectTags.count];
    [self.collectionView reloadData];
}


#pragma mark - getter

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = ColorHex(0x2E2E2E);
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text=@"选择你擅长话题(0/3)";
    }
    return _titleLabel;
}


-(UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        float fw = (LL_ScreenWidth-2*15-3*10)/3;
        float fh = 30;
        layout.itemSize = CGSizeMake(fw, fh);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        [_collectionView registerClass:[TagSelectCollectionCell class] forCellWithReuseIdentifier:TagSelectCollectionCellID];
    }
    return _collectionView;
}

-(NSMutableArray *)selectTags
{
    if (_selectTags==nil) {
        _selectTags = [[NSMutableArray alloc]init];
    }
    
    return _selectTags;
}

-(BOOL)isInSelecteTag:(NSString*)name
{
    for (int i=0; i<self.selectTags.count; i++)
    {
        NSString* str = [self.selectTags objectAtIndex:i];
        if ([str isEqualToString:name]) {
            return YES;
        }
    }
    
    return NO;
}

@end
